<?php

namespace Cck\ArticlesSystem\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Cck\ArticlesSystem\Http\Models\Category;

class CategoryStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name' => [
                'required',
                'max:255',
                Rule::unique('categories', 'name')->whereNull('deleted_at')
            ],
            'slug' => [
                'required_unless:url_type,' . Category::URL_TYPE_ID,
                'max:255',
                Rule::unique('categories', 'slug')
            ],
            'url_type' => [
                'required',
                Rule::in(array_keys(Category::getUrlTypeOptions()))
            ],
            'order' => 'integer|min:0',
            'seo_title' => 'max:255',
            'seo_keywords' => 'max:255',
            'seo_description' => 'max:500',
            'show_in_menus' => 'boolean',
            'page' => 'boolean',
            'page_html' => 'max:50000'
        ];

        if ($this->input('parent_id') > 0) {
            $rules['parent_id'] = [
                'integer',
                'min:0',
                Rule::exists('categories', 'id')->whereNull('deleted_at')
            ];
        }

        return $rules;
    }
}
